package com.cskaoyan.wordmemorize.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cskaoyan.wordmemorize.converter.AdminLogConverter;
import com.cskaoyan.wordmemorize.dao.entity.OperateLogDO;
import com.cskaoyan.wordmemorize.dao.mapper.OperateLogMapper;
import com.cskaoyan.wordmemorize.dto.admin.OperateLogDTO;
import com.cskaoyan.wordmemorize.dto.admin.PageDTO;
import com.cskaoyan.wordmemorize.request.OperateLogPageRequest;
import com.cskaoyan.wordmemorize.service.AdminLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * 操作日志服务实现类
 */
@Service
public class AdminLogServiceImpl implements AdminLogService {

    @Autowired
    private OperateLogMapper operateLogMapper;

    @Autowired
    private AdminLogConverter adminLogConverter;

    @Override
    public PageDTO<OperateLogDTO> getOperateLogPage(OperateLogPageRequest request) {
        // 构建查询条件
        LambdaQueryWrapper<OperateLogDO> wrapper = new LambdaQueryWrapper<>();

        // 按员工ID筛选
        if (StringUtils.hasText(request.getEmployeeId())) {
            wrapper.eq(OperateLogDO::getEmployeeId, Long.valueOf(request.getEmployeeId()));
        }

        // 按数据类型筛选
        if (StringUtils.hasText(request.getDataType())) {
            wrapper.eq(OperateLogDO::getDataType, request.getDataType());
        }

        // 按关键词筛选（在data字段中搜索）
        if (StringUtils.hasText(request.getKeyword())) {
            wrapper.like(OperateLogDO::getData, request.getKeyword());
        }

        // 按创建时间倒序排列
        wrapper.orderByDesc(OperateLogDO::getCreateTime);

        // 分页查询
        Page<OperateLogDO> page = new Page<>(request.getPageNum(), request.getCount());
        Page<OperateLogDO> result = operateLogMapper.selectPage(page, wrapper);

        // 转换为DTO
        return adminLogConverter.operationLogPage2PageDTO(result.getRecords(), result.getTotal());
    }
}